/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Organisations.java
 *
 * Created on Aug 22, 2009, 11:04:34 PM
 */

package org.plotnikov.pricemaker.preferences;

import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.ListSelectionModel;
import org.jdesktop.application.Action;
import org.plotnikov.pricemaker.db.DBOperations;
import org.plotnikov.pricemaker.db.SQLiteDBOperationsImpl;
import org.plotnikov.pricemaker.error.DBException;
import org.plotnikov.pricemaker.error.RootException;
import org.plotnikov.pricemaker.utils.DBEntity;
import org.plotnikov.pricemaker.gui.utils.GUIUtils;

/**
 *
 * @author alexey
 */
public class ProductionDialog extends javax.swing.JDialog {

    private DBOperations db = null;

    /** Creates new form Organisations */
    public ProductionDialog(java.awt.Frame parent, boolean modal, DBOperations db) {
        super(parent, modal);
        initComponents();
        jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList4.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList5.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.db = db;
        refreshOrganisations();
        refreshTitleProduct();
        refreshProducts();
        refreshPostavchiki();
        refreshPriceFor();
        setVisible(true);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jTextField2 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jTextField3 = new javax.swing.JTextField();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList4 = new javax.swing.JList();
        jTextField4 = new javax.swing.JTextField();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();
        jButton17 = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jList5 = new javax.swing.JList();
        jTextField5 = new javax.swing.JTextField();
        jButton18 = new javax.swing.JButton();
        jButton19 = new javax.swing.JButton();
        jButton20 = new javax.swing.JButton();
        jButton21 = new javax.swing.JButton();
        jButton13 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.plotnikov.pricemaker.PriceMakerApp.class).getContext().getResourceMap(ProductionDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Организации"));
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setName("jList1"); // NOI18N
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jTextField1.setName("jTextField1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.plotnikov.pricemaker.PriceMakerApp.class).getContext().getActionMap(ProductionDialog.class, this);
        jButton1.setAction(actionMap.get("addOrganisation")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("editOrganisation")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("deleteOrganisation")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jButton4.setAction(actionMap.get("refreshOrganisations")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jButton4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton3))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton3)
                    .add(jButton2)
                    .add(jButton1)
                    .add(jButton4))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Наименование товара"));
        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList2.setName("jList2"); // NOI18N
        jList2.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList2ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList2);

        jTextField2.setName("jTextField2"); // NOI18N

        jButton5.setAction(actionMap.get("refreshTitleProduct")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N

        jButton6.setAction(actionMap.get("addTitleProduct")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N

        jButton7.setAction(actionMap.get("editTitleProduct")); // NOI18N
        jButton7.setName("jButton7"); // NOI18N

        jButton8.setAction(actionMap.get("deleteTitleProduct")); // NOI18N
        jButton8.setName("jButton8"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jButton5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton6)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton7)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton8))
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton5)
                    .add(jButton6)
                    .add(jButton7)
                    .add(jButton8))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel3.border.title"))); // NOI18N
        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        jList3.setName("jList3"); // NOI18N
        jList3.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList3ValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(jList3);

        jTextField3.setName("jTextField3"); // NOI18N

        jButton9.setAction(actionMap.get("refreshProducts")); // NOI18N
        jButton9.setName("jButton9"); // NOI18N

        jButton10.setAction(actionMap.get("addProduction")); // NOI18N
        jButton10.setName("jButton10"); // NOI18N

        jButton11.setAction(actionMap.get("editProduction")); // NOI18N
        jButton11.setName("jButton11"); // NOI18N

        jButton12.setAction(actionMap.get("deleteProduction")); // NOI18N
        jButton12.setName("jButton12"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                        .add(jButton9)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton10)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton11)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton12))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                    .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton12)
                    .add(jButton11)
                    .add(jButton10)
                    .add(jButton9))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(4, 4, 4))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(47, 47, 47))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel4.TabConstraints.tabTitle"), jPanel4); // NOI18N

        jPanel5.setName("jPanel5"); // NOI18N

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel6.border.title"))); // NOI18N
        jPanel6.setName("jPanel6"); // NOI18N

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        jList4.setName("jList4"); // NOI18N
        jList4.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList4ValueChanged(evt);
            }
        });
        jScrollPane4.setViewportView(jList4);

        jTextField4.setName("jTextField4"); // NOI18N

        jButton14.setAction(actionMap.get("refreshPriceFor")); // NOI18N
        jButton14.setName("jButton14"); // NOI18N

        jButton15.setAction(actionMap.get("addPriceFor")); // NOI18N
        jButton15.setName("jButton15"); // NOI18N

        jButton16.setAction(actionMap.get("editPriceFor")); // NOI18N
        jButton16.setName("jButton16"); // NOI18N

        jButton17.setAction(actionMap.get("deletePriceFor")); // NOI18N
        jButton17.setName("jButton17"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel6Layout = new org.jdesktop.layout.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel6Layout.createSequentialGroup()
                        .add(jButton14)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton15)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton16)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton17))
                    .add(jTextField4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel6Layout.createSequentialGroup()
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel6Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton14)
                    .add(jButton15)
                    .add(jButton16)
                    .add(jButton17))
                .addContainerGap())
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel7.border.title"))); // NOI18N
        jPanel7.setName("jPanel7"); // NOI18N

        jScrollPane5.setName("jScrollPane5"); // NOI18N

        jList5.setName("jList5"); // NOI18N
        jList5.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList5ValueChanged(evt);
            }
        });
        jScrollPane5.setViewportView(jList5);

        jTextField5.setName("jTextField5"); // NOI18N

        jButton18.setAction(actionMap.get("refreshPostavchiki")); // NOI18N
        jButton18.setName("jButton18"); // NOI18N

        jButton19.setAction(actionMap.get("addPostavchik")); // NOI18N
        jButton19.setName("jButton19"); // NOI18N

        jButton20.setAction(actionMap.get("editPostavchik")); // NOI18N
        jButton20.setName("jButton20"); // NOI18N

        jButton21.setAction(actionMap.get("deletePostavchik")); // NOI18N
        jButton21.setName("jButton21"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel7Layout.createSequentialGroup()
                        .add(jButton18)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton19)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jButton20)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton21))
                    .add(jTextField5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE)
                    .add(jScrollPane5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel7Layout.createSequentialGroup()
                .add(jScrollPane5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton18)
                    .add(jButton19)
                    .add(jButton20)
                    .add(jButton21))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .add(jPanel7, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel5Layout.createSequentialGroup()
                .add(jPanel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .add(jPanel5Layout.createSequentialGroup()
                .add(jPanel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(279, 279, 279))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel5.TabConstraints.tabTitle"), jPanel5); // NOI18N

        jButton13.setAction(actionMap.get("close")); // NOI18N
        jButton13.setText(resourceMap.getString("jButton13.text")); // NOI18N
        jButton13.setName("jButton13"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 672, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(603, Short.MAX_VALUE)
                .add(jButton13)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 530, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton13)
                .add(10, 10, 10))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        Object obj = jList1.getSelectedValue();
        if (obj!=null)
            jTextField1.setText(obj.toString());
        refreshProducts();
    }//GEN-LAST:event_jList1ValueChanged

    private void jList2ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList2ValueChanged
        Object obj = jList2.getSelectedValue();
        if (obj!=null)
            jTextField2.setText(obj.toString());
        refreshProducts();
    }//GEN-LAST:event_jList2ValueChanged

    private void jList3ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList3ValueChanged
        Object obj = jList3.getSelectedValue();
        if (obj!=null)
            jTextField3.setText(obj.toString());
    }//GEN-LAST:event_jList3ValueChanged

    private void jList4ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList4ValueChanged
        Object obj = jList4.getSelectedValue();
        if (obj!=null)
            jTextField4.setText(obj.toString());
}//GEN-LAST:event_jList4ValueChanged

    private void jList5ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList5ValueChanged
        Object obj = jList5.getSelectedValue();
        if (obj!=null)
            jTextField5.setText(obj.toString());
}//GEN-LAST:event_jList5ValueChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ProductionDialog dialog = new ProductionDialog(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void refreshOrganisations() {
        if (db!=null){
            try {
                int sel = jList1.getSelectedIndex();
                Vector<DBEntity> list = db.listOrganisations();
                jList1.setModel(new ListModel(list));
                if (sel!=-1)
                    jList1.setSelectedIndex(sel);
            } catch (DBException ex) {
                Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void editOrganisation() {
        if (db!=null){
            Object obj = jList1.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.updateOrganisation(e.getInt("id"), jTextField1.getText());
                        refreshOrganisations();
                    } catch (RootException ex) {
                        Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void addOrganisation() {
        if (db!=null){
            try {
                db.addOrganisation(jTextField1.getText());
                refreshOrganisations();
            } catch (DBException ex) {
                Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void deleteOrganisation() {
        if (db!=null){
            Object obj = jList1.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.deleteOrganisation(e.getInt("id"));
                        refreshOrganisations();
                    } catch (RootException ex) {
                        Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void refreshPostavchiki() {
        if (db!=null){
            try {
                int sel = jList5.getSelectedIndex();
                Vector<DBEntity> list = db.listPostavchiki();
                jList5.setModel(new ListModel(list));
                if (sel!=-1)
                    jList5.setSelectedIndex(sel);
            } catch (DBException ex) {
                ex.printStackTrace();
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void addPostavchik() {
        if (db!=null){
            try {
                db.addPostavchik(jTextField5.getText());
                refreshPostavchiki();
            } catch (DBException ex) {
                ex.printStackTrace();
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void editPostavchik() {
        if (db!=null){
            Object obj = jList5.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.updatePostavchik(e.getInt("id"), jTextField5.getText());
                        refreshPostavchiki();
                    } catch (RootException ex) {
                        ex.printStackTrace();
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void deletePostavchik() {
        if (db!=null){
            Object obj = jList5.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.deletePostavchik(e.getInt("id"));
                        refreshPostavchiki();
                    } catch (RootException ex) {
                        ex.printStackTrace();
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void refreshPriceFor() {
        if (db!=null){
            try {
                int sel = jList4.getSelectedIndex();
                Vector<DBEntity> list = db.listPricesFor();
                jList4.setModel(new ListModel(list));
                if (sel!=-1)
                    jList4.setSelectedIndex(sel);
            } catch (DBException ex) {
                ex.printStackTrace();
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void addPriceFor() {
        if (db!=null){
            try {
                db.addPriceFor(jTextField4.getText());
                refreshPriceFor();
            } catch (DBException ex) {
                ex.printStackTrace();
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void editPriceFor() {
        if (db!=null){
            Object obj = jList4.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.updatePriceFor(e.getInt("id"), jTextField4.getText());
                        refreshPriceFor();
                    } catch (RootException ex) {
                        ex.printStackTrace();
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void deletePriceFor() {
        if (db!=null){
            Object obj = jList4.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.deletePriceFor(e.getInt("id"));
                        refreshPriceFor();
                    } catch (RootException ex) {
                        ex.printStackTrace();
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton18;
    private javax.swing.JButton jButton19;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton20;
    private javax.swing.JButton jButton21;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JList jList4;
    private javax.swing.JList jList5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables

    
    @Action
    public void refreshTitleProduct() {
        if (db!=null){
            try {
                int sel = jList2.getSelectedIndex();
                Vector<DBEntity> list = db.listTitleProducts();
                jList2.setModel(new ListModel(list));
                if (sel!=-1)
                    jList2.setSelectedIndex(sel);
            } catch (DBException ex) {
                Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void addTitleProduct() {
        if (db!=null){
            try {
                db.addTitleProduction(jTextField2.getText());
                refreshTitleProduct();
            } catch (DBException ex) {
                Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void editTitleProduct() {
        if (db!=null){
            Object obj = jList2.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.updateTitleProduction(e.getInt("id"), jTextField2.getText());
                        refreshTitleProduct();
                    } catch (RootException ex) {
                        Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void deleteTitleProduct() {
        if (db!=null){
            Object obj = jList2.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.deleteTitleProduction(e.getInt("id"));
                        refreshTitleProduct();
                    } catch (RootException ex) {
                        Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void refreshProducts() {
        if (db!=null){
            try {
                int sel = jList3.getSelectedIndex();
                Vector<DBEntity> list;
                Object id_org = jList1.getSelectedValue();
                Object id_type = jList2.getSelectedValue();
                if (id_org==null || id_type==null){
                    list = new Vector<DBEntity>(0);
                }else{
                    list = db.listProducts(((DBEntity)id_org).getInt("id"), ((DBEntity)id_type).getInt("id"));
                }
                jList3.setModel(new ListModel(list));
                if (sel!=-1)
                    jList3.setSelectedIndex(sel);
            } catch (DBException ex) {
                Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                GUIUtils.showError(ex.getMessage());
            } catch (RootException ex) {
                Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void addProduction() {
        if (db!=null){
            try {
                Object id_org = jList1.getSelectedValue();
                Object id_type = jList2.getSelectedValue();
                if (id_org==null)
                    throw new RootException("Выберите организацию");
                if (id_type==null)
                    throw new RootException("Выберите наименование товара");
                db.addProduction(
                        ((DBEntity)id_org).getInt("id"),
                        ((DBEntity)id_type).getInt("id"),
                        jTextField3.getText());
                refreshProducts();
            } catch (DBException ex) {
                Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                GUIUtils.showError(ex.getMessage());
            }catch (RootException ex){
                GUIUtils.showError(ex.getMessage());
            }
        }
    }

    @Action
    public void editProduction() {
        if (db!=null){
            Object obj = jList3.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.updateProduction(e.getInt("id"), jTextField3.getText());
                        refreshProducts();
                    } catch (RootException ex) {
                        Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void deleteProduction() {
        if (db!=null){
            Object obj = jList3.getSelectedValue();
            if (obj!=null){
                if (obj instanceof DBEntity){
                    DBEntity e = (DBEntity) obj;
                    try {
                        db.deleteProduction(e.getInt("id"));
                        refreshProducts();
                    } catch (RootException ex) {
                        Logger.getLogger(ProductionDialog.class.getName()).log(Level.SEVERE, null, ex);
                        GUIUtils.showError(ex.getMessage());
                    }
                }
            }
        }
    }

    @Action
    public void close() {
        this.dispose();
    }

}

class ListModel extends AbstractListModel{

    private Vector<DBEntity> data;

    public ListModel(Vector<DBEntity> data) {
        this.data = data;
    }

    public int getSize() {
        return data.size();
    }

    public Object getElementAt(int index) {
        return data.get(index);
    }

}